package lib.model;

import geor.app.R;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public abstract class Helper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "geor.db";
	private static final int VERSION = 2;
	private static final String DROP_IF_EXISTS = "DROP TABLE IF EXISTS ";
	protected Resources resources = null;
	
	public Helper(Context context) {
		super(context, DATABASE_NAME, null, VERSION);
		resources = context.getResources();
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(resources.getString(R.string.sql_conta_create_table));
		db.execSQL(resources.getString(R.string.sql_contas_pagar_create_table));
		db.execSQL(resources.getString(R.string.sql_contas_pagar_parcela_create_table));
		// db.execSQL(resources.getString(R.string.sql_resumo_contas_create_table)); @deprecated
		db.execSQL(resources.getString(R.string.sql_conta_insert_default));
		db.execSQL(resources.getString(R.string.sql_compra_helper_dump));
		db.execSQL(resources.getString(R.string.sql_compra_helper_dump1));
		db.execSQL(resources.getString(R.string.sql_compra_helper_dump_1));
		db.execSQL(resources.getString(R.string.sql_compra_helper_dump_11));
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int odlVersion, int newVersion) {
		Log.i("Helper", "droping tables ...");
		db.execSQL(DROP_IF_EXISTS + "contas_pagar_parcela;");
		db.execSQL(DROP_IF_EXISTS + "contas_pagar;");
		db.execSQL(DROP_IF_EXISTS + "conta;");
		onCreate(db);
	}
	
	/**
	 * insert a new row in the database and return the _id.
	 * insere uma nova coluna no banco de dados e retorna o _id.
	 * @param values
	 * @return
	 */
	public abstract long create(ContentValues values);
	
	/**
	 * read a row in the database with following the id.
	 * lê uma coluna no banco de dados com o seguinte id.
	 * @param id
	 * @return
	 */
	public abstract Cursor read(String id);
	
	/**
	 * update existing row in the database.
	 * atualiza uma coluna existente no banco de dados.
	 * @param id
	 * @param values
	 */
	public abstract void update(String id, ContentValues values);
	
	/**
	 * delete an existing row in the database.
	 * deleta uma coluna existente no banco de dados.
	 * @param id
	 * @return
	 */
	public abstract boolean delete(String id);
	
	/**
	 * insert a new row in the database.
	 * insere uma nova coluna no banco de dados.
	 * @param table
	 * @param nullColumnRack
	 * @param values
	 */
	protected long create(String table, String nullColumnRack, ContentValues values) {
		return getWritableDatabase().insert(table, nullColumnRack, values);
	}
	
	/**
	 * delete an existing row in the database.
	 * deleta uma coluna existente no banco de dados.
	 * @param table
	 * @param id
	 * @return
	 */
	protected int delete(String table, String id) {
		String whereClause = "_id = ?";
		String[] whereArgs = {id};
		return getWritableDatabase().delete(table, whereClause, whereArgs);
	}
	
	public int update(String table, ContentValues values, String id) {
		String whereClause = "_id = ?";
		String[] whereArgs = {id};
		return getWritableDatabase().update(table, values, whereClause, whereArgs);
	}
	
	/**
	 * read all the rows from the database where the clause aplies.
	 * lê todas as linhas do banco de dados onde a restrição se aplica.
	 * @param whereClause
	 * @param whereArgs
	 * @return
	 */
	public abstract Cursor readAll(String whereClause, String[] whereArgs);
	
	public SQLiteDatabase getDatabase() {
		return getWritableDatabase(); 
	}
	
	public abstract Cursor contas_Mes(String data);
	public abstract Cursor contas_Pagar(String data);

}
